package zhuzx.solution.dbt;

import org.zhuzx.util.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author zhuzx 2022/7/19
 */
public class DbtChannelInitializer {

	private static final AtomicInteger id = new AtomicInteger(100);
	private static final AtomicInteger rank = new AtomicInteger(1);
	private static final String createTime = "2022-07-19 12:12:12";

	public static void main(String[] args) throws IOException {

		String dir = "C:\\Users\\Administrator\\Desktop\\渠道初始化\\";
		List<Map<String, String>> channelList = ExcelUtil.createListMapSSFromExcel(
				Files.newInputStream(Paths.get(dir + "channel.xlsx")), true, 0);
		List<Map<String, String>> refList = ExcelUtil.createListMapSSFromExcel(
				Files.newInputStream(Paths.get(dir + "关联数据.xlsx")), true, 0);
		List<Map<String, String>> pubList = ExcelUtil.createListMapSSFromExcel(
				Files.newInputStream(Paths.get(dir + "在线产品清单.xlsx")), true, 0);

		List<Map<String,String>> channelDatasOri = channelList.stream().map(
				item -> createChannel(item.get("channelcode"), item.get("channelname"))
		).collect(Collectors.toList());
		Map<String, Map<String, String>> channelIdMappingCha = channelDatasOri.stream().collect(
				Collectors.toMap(item -> item.get("code"), item -> item,
						(item1, item2) -> item1, LinkedHashMap::new));
		//剔除重复
		List<Map<String,String>> channelDatas = new ArrayList<>(channelIdMappingCha.values());

		Map<String, Map<String, String>> dbtIdMappingPub = pubList.stream().collect(
				Collectors.toMap(item -> item.get("dbt_id"), item -> item));
		//每个产品一个自增器，关联数据都从1开始排。
		Map<String, AtomicInteger> dbtIdMappingRank = pubList.stream().collect(
				Collectors.toMap(item -> item.get("dbt_id"), item -> new AtomicInteger(1)));
		List<Map<String, String>> refDatas = refList.stream().filter(item ->
				{
					String code = item.get("code");
					String wrong = "google";
					if (code != null && code.startsWith(wrong)) {
						String dbtId = item.get("dbt_id");
						if (dbtIdMappingPub.containsKey(dbtId)) {
							System.out.println(code + "：" + dbtId);
						}
					}
					return
					dbtIdMappingPub.containsKey(item.get("dbt_id")) && GeneralUtil.isNotBlank(item.get("code"));
				}
		).map(item -> {
			Map<String, String> row = new LinkedHashMap<>();
			String dbtId = item.get("dbt_id");
			Map<String, String> pub = dbtIdMappingPub.get(dbtId);
			row.put("publish_id", pub.get("publish_id"));
			String code = item.get("code");
			Map<String, String> channel = channelIdMappingCha.get(code);
			if (channel == null) {//这个渠道code在渠道字典数据中没有，则需要插入字典。
				channel = createChannel(code, code);//name也用code填充
				channelIdMappingCha.put(code, channel);
				channelDatas.add(channel);
			}
			row.put("channel_id", channel.get("id"));
			row.put("online_name", pub.get("online_name"));
			row.put("pub_status", pub.get("pub_status"));
			row.put("rank", String.valueOf(dbtIdMappingRank.get(dbtId).getAndIncrement()));
			row.put("create_user", TextConstants.SYS_AUTO);
			row.put("create_time", createTime);
			row.put("update_time", createTime);
			return row;
		}).collect(Collectors.toList());
		FileOperationUtil.writeToFile(
				JdbcUtil.createBatchInsertSql("basic_db_channel", channelDatas).getBytes(StandardCharsets.UTF_8),
				"basic_db_channel初始化数据.sql", true);
		FileOperationUtil.writeToFile(
				JdbcUtil.createBatchInsertSql("publish_ref_channel", refDatas).getBytes(StandardCharsets.UTF_8),
				"publish_ref_channel初始化数据.sql", true);
	}
	
	private static Map<String,String> createChannel(String code, String name) {
		Map<String,String> row = new LinkedHashMap<>();
		row.put("id", String.valueOf(id.getAndIncrement()));
		row.put("code", code);
		row.put("name", name);
		row.put("market_code", "1");
		row.put("rank", String.valueOf(rank.getAndIncrement()));
		row.put("create_user", TextConstants.SYS_AUTO);
		row.put("create_time", createTime);
		row.put("update_time", createTime);
		return row;
	}
	
}
